package GUI;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import Client.ChatClientSendThread;
import Mailbox.Mailbox;

public class GUIcreatechatroom extends JFrame implements ActionListener {

	private static final long serialVersionUID = 27L;
	private JPanel panel; // The panel
	private JButton createButton; // The login button
	private JLabel infoLabel; // The label for the chat system
	private JTextField chatroomField; // Text field for the username
	private JLabel chatroomLabel; // The label for the username
	ChatClientSendThread client;
	private GUIstartpage sp;

	public GUIcreatechatroom(ChatClientSendThread client, GUIstartpage sp) {
		this.client = client;
		this.sp = sp;

		panel = new JPanel();
		panel.setLayout(new GridBagLayout());

		createButton = new JButton("Create your new chatroom!");
		infoLabel = new JLabel("Write the name of the new chatroom:");
		chatroomField = new JTextField("", 15);
		chatroomLabel = new JLabel("Name:");

		createButton.addActionListener(this); // Listens to the login button

		infoLabel.setFont(new Font("Arial", Font.BOLD, 16));
		infoLabel.setForeground(new Color(242, 128, 161));

		// The infoLabel
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 2;
		c.insets = new Insets(10, 0, 50, 0);
		panel.add(infoLabel, c);

		// The chatroom label
		c = new GridBagConstraints();
		c.gridx = 1;
		c.gridy = 3;
		c.anchor = GridBagConstraints.LINE_START;
		c.insets = new Insets(0, 0, 10, 0);
		panel.add(chatroomLabel, c);

		// The name field
		c.gridx = 1;
		c.gridy = 3;
		c.anchor = GridBagConstraints.LINE_END;
		panel.add(chatroomField, c);

		// The create button
		c.gridx = 0;
		c.gridy = 8;
		c.gridwidth = 4;
		c.insets = new Insets(20, 0, 0, 0);
		c.anchor = GridBagConstraints.PAGE_END;
		c.fill = GridBagConstraints.BOTH;
		c.ipady = 5;
		panel.add(createButton, c);

		add(panel);

		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setLocation(200, 200);
		setSize(400, 400);
		setVisible(true);
		setResizable(false);

	}


	public String getChatRoomName() {
		return chatroomField.getText();
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		String roomName = getChatRoomName();

		if (roomName.equals("")) {
			JOptionPane.showMessageDialog(this,
					"Write a name for your new chatroom!", null, 2);
		} else {
			this.setVisible(false);
			sp.addChatroom(roomName);
			client.send("C " + roomName);
		}
	}

}
